package com.crm.role;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.DispatchActionSupport;

import com.crm.role.po.Role;

public class RoleAction  extends DispatchActionSupport {
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleForm roleForm = (RoleForm) form;// TODO Auto-generated method stub
		ApplicationContext context= this.getWebApplicationContext();
		IRoleDAO dao = (IRoleDAO)context.getBean("roledao");
		Role role = new Role();
		try {
			BeanUtils.copyProperties(role,roleForm);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List list = dao.getRoleList(role);
		System.out.println("list.size()="+list.size());
		request.setAttribute("rolelist", list);
		return mapping.findForward("rolelist");
	}	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleForm roleForm = (RoleForm) form;// TODO Auto-generated method stub
		ApplicationContext context= this.getWebApplicationContext();
		IRoleDAO dao = (IRoleDAO)context.getBean("roledao");

		
		Role role = dao.getRole(roleForm.getId());
		
		try {
			BeanUtils.copyProperties(roleForm,role);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List list = dao.getPowerListin(roleForm.getId());
		List notlist = dao.getPowerListnotin(roleForm.getId());
		request.setAttribute("powerlistin", list);
		request.setAttribute("powerlistnotin", notlist);
		return mapping.findForward("roledetail");
	}
	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		RoleForm roleForm = (RoleForm) form;// TODO Auto-generated method stub
		ApplicationContext context= this.getWebApplicationContext();
		IRoleDAO dao = (IRoleDAO)context.getBean("roledao");
		Role role = new Role();
		try {
			BeanUtils.copyProperties(role,roleForm);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dao.modifyRole(roleForm.getSelect(), roleForm.getId());
		List list = dao.getRoleList(role);
		System.out.println("list.size()="+list.size());
		request.setAttribute("rolelist", list);
		return mapping.findForward("rolelist");
	}	
}
